package top.zhongyy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import top.zhongyy.pojo.Tag;
import top.zhongyy.mapper.TagMapper;
import top.zhongyy.service.TagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Andy
 * @since 2021-03-25
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    @Autowired
    private TagMapper tagMapper;
    
    @Override
    public Tag getTagByName(String TagName) {
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        wrapper.eq("name", TagName);
        return tagMapper.selectOne(wrapper);
    }
    
    @Override
    public List<Tag> parseTagIds(String ids) {
        String[] split = ids.split(",");
        List<Tag> tags = new ArrayList<>();
        for (String s : split) {
            tags.add(tagMapper.selectById(Long.parseLong(s)));
        }
        return tags;
    }
    
    @Override
    public int TagBlogCount(Tag tag) {
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        wrapper.eq("tags_id", tag.getId());
        return tagMapper.selectCount(wrapper);
    }
    
    @Override
    public List<Tag> getAllTags() {
        return tagMapper.getAllTags();
    }
}
